package algos;


public class GoodTuringEstimator extends EstimationFunction {

    private final Function<Long, Double> s;

    public GoodTuringEstimator(NGramModel model, Function<Long, Double> smoothMethod) {
        super(model);
        this.s = smoothMethod;
    }
    
    @Override
    public double p(int wordIndex) {
        long r = model.predict(wordIndex);
        if (r > 0) {
            return (r + 1) * s.f(r + 1) / s.f(r) / model.getNTrains();
        } else {
            return ((double)model.getNr(1)) / model.getNr(0) / model.getNTrains();
        }
    }

}
